/**
 * @description: 防抖函数
 * @param {Function} func
 * @param {number} delay
 * @return {Function}
 * */
export function debounce(func, delay = 300) {
  let timeout;

  return function (...args) {
    const context = this;
    clearTimeout(timeout);
    timeout = setTimeout(() => {
      func.apply(context, args);
    }, delay);
  };
}

/**
 * 根据后缀获取文件类型
 * @param {*} ext
 * */
export function getFileTypeWithExt(ext) {
  if (ext) {
    ext = ext.toLowerCase();
    if (['jpg', 'png', 'jpeg', 'bmp', 'ico', 'gif'].includes(ext)) {
      return 'image';
    } else if (ext === 'psd') {
      return 'psd';
    } else if (ext === 'tif') {
      return 'tif';
    } else if (['mp4', 'm2v', 'mkv', 'rmvb', 'wmv', 'avi', 'flv', 'mov', '3gp'].includes(ext)) {
      return 'video';
    } else if (['mp3', 'wma', 'wav'].includes(ext)) {
      return 'audio';
    } else if (['xlsx', 'xls'].includes(ext)) {
      return 'excel';
    } else if (['doc', 'docx'].includes(ext)) {
      return 'word';
    } else if (['rar', 'zip', '7z', 'tar', 'iso', 'dmg'].includes(ext)) {
      return 'archive';
    } else if (ext === 'pdf') {
      return 'pdf';
    } else if (['ppt', 'pptx'].includes(ext)) {
      return 'ppt';
    } else if (['txt', 'text'].includes(ext)) {
      return 'text';
    }
  }
  return '';
}

/**
 * 根据后缀获取文件图标
 * @param {*} ext
 * */
const fileIconMap = {
  image: '/static/images/icons/file_img.png',
  tif: '/static/images/icons/file_tif.png',
  video: '/static/images/icons/file_video.png',
  audio: '/static/images/icons/file_music.png',
  excel: '/static/images/icons/file_excle.png',
  word: '/static/images/icons/file_word.png',
  archive: '/static/images/icons/file_zip.png',
  pdf: '/static/images/icons/file_pdf.png',
  ppt: '/static/images/icons/file_ppt.png',
  text: '/static/images/icons/file_txt.png',
};
export function getFileIconWithSuffix(ext) {
  const fileType = getFileTypeWithExt(ext);
  const fileIcon = fileIconMap[fileType] || '/static/images/icons/file_unknown.png';

  return {
    fileType,
    fileIcon,
  };
}
/**
 * 获取文件类型图标
 * @param {*} file
 * */
export function getFileTypeIcon(file) {
  if (file.type.indexOf('image') !== -1) {
    return getFileIconWithSuffix('png');
  } else if (file.type.indexOf('audio') !== -1) {
    return getFileIconWithSuffix('mp3');
  } else if (file.type.indexOf('video') !== -1) {
    return getFileIconWithSuffix('mp4');
  } else {
    if (file.name) {
      const index = file.name.lastIndexOf('.');
      const ext = file.name.substr(index + 1) || '';
      return getFileIconWithSuffix(ext);
    } else if (file.file_name) {
      const index = file.file_name.lastIndexOf('.');
      const ext = file.file_name.substr(index + 1) || '';
      return getFileIconWithSuffix(ext);
    }
  }
}

/**
 * 根据文件路径获取文件类型图标
 * @param {string} fileUrl
 * */
export function getFileIconByFileUrl(fileUrl) {
  const ext = fileUrl.substring(fileUrl.lastIndexOf('.') + 1);
  return getFileIconWithSuffix(ext);
}

// 数字补位
const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : '0' + n;
};

// 日期格式化
export function formatDateTime(date, format = 'YYYY-MM-DD') {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  if (format == 'YYYY/MM/DD') {
    return `${[year, month, day].map(formatNumber).join('/')}`;
  } else if (format == 'YYYY-MM-DD hh:mm:ss') {
    return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`;
  } else if (format == 'YYYY-MM') {
    return `${[year, month].map(formatNumber).join('-')}`;
  } else if (format == 'MM-DD') {
    return `${[month, day].map(formatNumber).join('-')}`;
  } else {
    return `${[year, month, day].map(formatNumber).join('-')}`;
  }
}
// 更新用户本地信息
export function setStorageUserInfo(userInfo) {
  uni.setStorageSync('userInfo', userInfo);
}

// 吐丝提示
export function showToast(title, icon = 'none', duration = 2000) {
  uni.showToast({
    title,
    icon,
    duration,
  });
}
// 确认登录弹窗
export function showLoginConfirm() {
  uni.showModal({
    title: '提示',
    content: '您还未登录，请先登录',
    confirmText: '去登录',
    cancelText: '稍候再说',
    success: function (res) {
      if (res.confirm) {
        uni.navigateTo({
          url: '/pages/login/index',
        });
      }
    },
  });
}
